package com.matace.keychain.data;

import android.content.Context;
import android.database.Cursor;
import android.content.ContentValues;

import com.matace.keychain.core.dataBaseBasic.DatabaseHelper;
import com.matace.keychain.core.dataBaseBasic.DatabaseConstants;


public class DatabaseManager {
    private DatabaseHelper dbHelper;
    private final Context context;

    public DatabaseManager(Context context) {
        this.dbHelper = DatabaseHelper.getInstance(context);
        this.context = context;
    }

    public boolean isNameOrEmailExists(String name, String email) {
        String tableName = DatabaseConstants.TABLE_USERS;
        String[] columns = {DatabaseConstants.COL_USER_ID};
        String selection = DatabaseConstants.COL_USER_NAME + " = ? OR " + DatabaseConstants.COL_USER_EMAIL + " = ?";
        String[] selectionArgs = {name, email};
        boolean exists = false;
        Cursor cursor = null;
        try {
            cursor = dbHelper.queryData(tableName, columns, selection, selectionArgs, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                exists = true;
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return exists;
    }

    // 插入用户数据
    public long insertUser(String name, String email, String password) {
        ContentValues values = new ContentValues();
        values.put(DatabaseConstants.COL_USER_NAME, name);
        values.put(DatabaseConstants.COL_USER_EMAIL, email);
        values.put(DatabaseConstants.COL_USER_PASSWORD, password);
        return dbHelper.insertData(DatabaseConstants.TABLE_USERS, values);
    }

    public boolean registerUser(String name, String email, String password) {
        // 检查用户名和邮箱是否已存在
        if (isNameOrEmailExists(name, email)) {
            return false;
        }

        // 注册用户
        insertUser(name, email, password);
        return true;
    }

    // 判断用户名和密码是否匹配
    public boolean isLoginUser(String name, String password) {
        String tableName = DatabaseConstants.TABLE_USERS;
        String[] columns = {DatabaseConstants.COL_USER_ID};
        String selection = DatabaseConstants.COL_USER_NAME + " =? AND " + DatabaseConstants.COL_USER_PASSWORD + " =?";
        String[] selectionArgs = {name, password};
        boolean exists = false;
        Cursor cursor = null;
        try {
            cursor = dbHelper.queryData(tableName, columns, selection, selectionArgs, null, null, null);
            if (cursor!= null && cursor.moveToFirst()) {
                exists = true;
            }       
        } finally {
            if (cursor!= null) {
                cursor.close();
            }
        }
        return exists;
    }

    public boolean isLoginEmail(String email, String password) {
        String tableName = DatabaseConstants.TABLE_USERS;
        String[] columns = {DatabaseConstants.COL_USER_ID}; // 查询ID即可，判断是否存在
        String selection = DatabaseConstants.COL_USER_EMAIL + " = ? AND " + DatabaseConstants.COL_USER_PASSWORD + " = ?";
        String[] selectionArgs = {email, password};
        boolean exists = false;
        Cursor cursor = null;
        try {
            cursor = dbHelper.queryData(tableName, columns, selection, selectionArgs, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                exists = true; // 如果查询到数据，则表示邮箱和密码匹配
            }
        } finally {
            if (cursor != null) {
                cursor.close(); // 确保关闭Cursor
            }
        }
        return exists;
    }

}
